﻿using SAD_Assignment1.DAO;
using SAD_Assignment1.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;

namespace SAD_Assignment1.BLO
{
    public class AccountBLO
    {
        public bool CheckLogin(string email, string password)
        {
            AccountDAO accDAO = new AccountDAO();
            return accDAO.CheckLogin(email, password);
        }

        public bool AddNewAccount(string email, string pass, string name, int port)
        {
            AccountDAO accDAO = new AccountDAO();
            int ID = accDAO.AddNewAccount(email, pass, name,"deactive");
            if (ID > 0)
            {
                sendActiveMail(email, port, ID, email);
            }
            return ID > 0;
        }

        public bool AddNewAccount(string email, string pass, string name, string roles)
        {
            AccountDAO accDAO = new AccountDAO();
            int ID = accDAO.AddNewAccount(email, pass, name, roles);            
            return ID > 0;
        }

        public bool IsEmailExist(string email)
        {
            AccountDAO accDAO = new AccountDAO();
            return accDAO.IsEmailExist(email);
        }

        private void sendEmail(string toEmail, string title, string body)
        {
            string from = "hackethicdemo@gmail.com";
            string pass = "nguyenthihong";
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential(from, pass),
                EnableSsl = true
            };
            client.Send(from, toEmail, title, body);
        }

        private void sendActiveMail(string toEmail, int port, int id, string email)
        {
            string title = "Thư kích hoạt từ PhotoVL";
            string body = "Xin quý khách click vào link bên dưới để kích hoạt tài khoản \n"
                + "http://localhost:"+port+"/Public/Register.aspx?active=&id=" + id + "&email=" + email;
            sendEmail(toEmail, title, body);
        }

        //public string getAccountRole(string email)
        //{
        //    AccountDAO accDAO = new AccountDAO();
        //    return accDAO.getAccountRole(email);
        //}

        public bool ActiveAccount(string email, int id)
        {
            AccountDAO accDAO = new AccountDAO();
            return accDAO.ActiveAccount(email, id);
        }

        public AccountDTO getAccountInfo(string email)
        {
            AccountDAO accDAO = new AccountDAO();
            return accDAO.getAccountInfo(email).First();
        }

        public UserDTO getUserInfo(int accID)
        {
            AccountDAO accDAO = new AccountDAO();
            List<UserDTO> list = accDAO.getUserInfo(accID);
            if (list.Count == 0) { return null; }
            return list.First();
        }

        public bool UpdateAccount(int accID, string email, string name, string pass, string roleName)
        {
            AccountDAO accDAO = new AccountDAO();
            return accDAO.UpdateAccount(accID, email, pass, name, roleName);
        }

        public List<AccountDTO> getAccountList()
        {
            AccountDAO accDAO = new AccountDAO();
            return accDAO.getAccountList();
        }

        public AccountDTO getAccountByID(int accID)
        {
            AccountDAO accDAO = new AccountDAO();
            List<AccountDTO> list = accDAO.getAccountByID(accID);
            if (list.Count == 0) { return null; }
            return list.First();
        }
    }
}